package cn.e3mall.sso.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.util.E3Result;
import cn.e3mall.common.util.JsonUtils;
import cn.e3mall.pojo.TbUser;
import cn.e3mall.sso.service.TokenService;

@Service
public class TokenServiceImpl implements TokenService {

	@Autowired
	JedisClient jedisClient;
	@Value("${SESSION_EXPIRE}")
	private Integer SESSION_EXPIRE; 
	
	@Override
	public E3Result getUserByToken(String token) {
		//根据token到redis中取得用户信息
		String json= jedisClient.get("SESSION:"+token);
		//取不到用户信息，登陆过期，返回登陆过期
		if(StringUtils.isBlank(json)){
			return E3Result.build(201, "用户登陆已经过期");
		}
		//取到用户信息更新token过期时间 
		jedisClient.expire("SESSION:"+token, SESSION_EXPIRE);
		//返回包涵User结果
		TbUser user = JsonUtils.jsonToPojo(json,TbUser.class);
		
		return E3Result.ok(user);
	}

}
